//
//  Experiment.cpp
//  GPUProcessingTesting
//
//  Created by kadlubek47 on 20.08.2013.
//  Copyright (c) 2013 kadlubek47. All rights reserved.
//

#include "Experiment.h"


const std::string Experiment::after_main_properties__mark_           = std::string ("-------------------\n");
const std::string Experiment::after_trialsSet_properties__mark_      = std::string ("***\n");
const std::string Experiment::before_trialsSet_properties__mark_     = std::string ("\n\n\n");



#pragma mark Construct, destroy

Experiment::Experiment(const std::string& title, const std::string& folderPath): title_(title), folderPath_(folderPath) {}

Experiment::~Experiment() {}



#pragma mark Writing to file

void Experiment::writeResultsTo_file   (bool ifEachTrialPrinted) {
    using namespace std;
    
    //Creating file for writing
    string filePath;
    filePath.append(folderPath_);
    filePath.append(title_);
    filePath.append(".txt");
    ofstream fs(filePath.c_str(), ios_base::out | ios_base::trunc);
    //.
    writeToStream(fs);
    fs.close();
}

void Experiment::writeResultsTo_stdcout(bool ifEachTrialPrinted) {
    writeToStream(std::cout, false);
}

void Experiment::writeToStream(std::ostream& outputStream, bool ifTrialsArrayPrint) {

    //Main properties
    //main properties
    std::map <std::string, std::pair<boost::any, std::string>  > copy = properties_.get_allProperties();
    for(std::map <std::string, std::pair<boost::any, std::string>  >::iterator it = copy.begin(); it != copy.end(); ++it)
        outputStream <<  it->first << ": " << it->second.second << "\n";
    //.
    
    //trials set properies set as main
    for(std::vector<std::string>::iterator it = trialsSet_propierty_set_as_mainProperty_.begin(); it != trialsSet_propierty_set_as_mainProperty_.end(); ++it)
        outputStream << *it  << ": " << trialsSets_[0].properties_.get(*it).second << "\n";
    //.
    //.
    outputStream  <<  after_main_properties__mark_;
    
    for ( std::vector<TrialsSet>::iterator it_trialSet = trialsSets_.begin(); it_trialSet != trialsSets_.end(); ++it_trialSet ) {
        outputStream  <<  before_trialsSet_properties__mark_;
        //TrialsSet properties
        for(std::vector<std::string>::iterator it = trialsSet_propierties_printed_.begin(); it != trialsSet_propierties_printed_.end(); ++it)
            outputStream <<  *it << ": " << it_trialSet->properties_.get(std::string(*it)).second << "\n";
        //.
        
        //Trials array
        if ( ifTrialsArrayPrint ) {
            outputStream    <<  after_trialsSet_properties__mark_;
            for(unsigned int i_trial = 0; i_trial < it_trialSet->get_trialsNumber(); ++i_trial)
                outputStream <<      it_trialSet->get_trial(i_trial)      << "\n";
        }
        //.
    }
    
}

void Experiment::writeResultsTo_file_makeRformat() {
    
    using namespace std;
    
    //Creating file for writing
    string filePath;
    filePath.append(folderPath_);
    filePath.append(title_);
    filePath.append(".r");
    ofstream outputStream(filePath.c_str(), ios_base::out | ios_base::trunc);
    //.
    
    //R
    outputStream << "setwd ( \"" << folderPath_ << "\")\n";
    //.
    
    //Main properties
    //main properties
    std::map <std::string, std::pair<boost::any, std::string>  > copy = properties_.get_allProperties();
    outputStream << "mainProperties <- \"";
    for(std::map <std::string, std::pair<boost::any, std::string>  >::iterator it = copy.begin(); it != copy.end(); ++it)
        outputStream <<  it->first << ": " << it->second.second << "\\n";
    //.
    //trials set properies set as main
    for(std::vector<std::string>::iterator it = trialsSet_propierty_set_as_mainProperty_.begin(); it != trialsSet_propierty_set_as_mainProperty_.end(); ++it)
        outputStream << *it  << ": " << trialsSets_[0].properties_.get(*it).second << "\\n";
    //.
    outputStream << "\"\n";
    //.
    
    outputStream << "trialsSets_properties <- vector( length = " << trialsSets_.size() << ")\n";
    outputStream << "trialsSets <- matrix (nrow = " << trialsSets_.size() << ", ncol = " << trialsSets_[0].get_trialsNumber() << ")\n";
    int i = 0;
    for ( std::vector<TrialsSet>::iterator it_trialSet = trialsSets_.begin(); it_trialSet != trialsSets_.end(); ++i, ++it_trialSet ) {
        
        //TrialsSet properties
        outputStream << "trialsSets_properties[" << i+1 << "] <- \"";
        for(std::vector<std::string>::iterator it = trialsSet_propierties_printed_.begin(); it != trialsSet_propierties_printed_.end(); ++it)
            outputStream <<  *it << ": " << it_trialSet->properties_.get(std::string(*it)).second << "\\n";
        outputStream << "\"\n";
        //.
        
        //Trials array
        outputStream  <<  "trialsSets["<< i+1 << ", ] <- c( ";
        for(unsigned int i_trial = 0; i_trial < it_trialSet->get_trialsNumber(); ++i_trial) {
            outputStream <<     it_trialSet->get_trial(i_trial);
            if(i_trial != it_trialSet->get_trialsNumber()-1 )
                outputStream << ", ";
        }
        outputStream <<  ")\n";
        //.
    }
    
    outputStream << "\npdf(file=\"after_R_process/" << title_ << ".pdf\")\n";
    outputStream << "library(gplots)\n";
    outputStream << "textplot(mainProperties,cex=1)\n";
    outputStream << "for(i in 1:length(trialsSets[,1])) {\n";
    outputStream << "\tplot(trialsSets[i, ])\n";
    outputStream << "\thist(trialsSets[i, ], breaks = 50 )\n";
    outputStream << "\ttextplot(trialsSets_properties[i])\n}\n";
    outputStream << "dev.off()\n";
}






